
/**
* @name: CrudServiceGenerator.java 
*
* @Copyright: (c) 2017 DLFC. All rights reserved. 
*
* @description: 
*
* @version: 1.0
* @date : 2017年1月16日 
* @author: Alex.Ge 
*
* @Modification  History:<br>
*  Date          Author         Version        Discription
*  2017年1月16日       Alex.Ge        1.0             <修改原因描述>
*/
package com.linlion.mybatis.generator.codegen.mybatis3.service;

import static org.mybatis.generator.internal.util.messages.Messages.getString;

import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.FullyQualifiedTable;
import org.mybatis.generator.api.dom.java.CompilationUnit;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.codegen.AbstractJavaGenerator;

import com.linlion.mybatis.generator.config.JavaServiceGeneratorConfiguration;

/**
 * @name: CrudServiceGenerator
 * @description:
 * 
 * @version 1.0
 * @author Alex.Ge
 *
 */
public class CrudServiceGenerator extends AbstractJavaGenerator {

	private JavaServiceGeneratorConfiguration configuration;

	public CrudServiceGenerator() {
		super();
	}

	public CrudServiceGenerator(JavaServiceGeneratorConfiguration javaServiceGeneratorConfiguration) {
		super();
		configuration = javaServiceGeneratorConfiguration;
	}

	@Override
	public List<CompilationUnit> getCompilationUnits() {

		FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
		progressCallback.startTask(getString("Progress.19", table.toString())); //$NON-NLS-1$

		CommentGenerator commentGenerator = context.getCommentGenerator();
		FullyQualifiedJavaType type = new FullyQualifiedJavaType(getServiceType());
		TopLevelClass topLevelClass = new TopLevelClass(type);
		topLevelClass.setVisibility(JavaVisibility.PUBLIC);
		commentGenerator.addJavaFileComment(topLevelClass);

		topLevelClass.addAnnotation("@Service");
		topLevelClass.addImportedType(new FullyQualifiedJavaType("org.springframework.stereotype.Service"));

		FullyQualifiedJavaType superClass = getSuperClass();
		if (superClass != null) {
			topLevelClass.addImportedType(new FullyQualifiedJavaType(configuration.getRootClass()));
			topLevelClass.addImportedType(new FullyQualifiedJavaType(configuration.getJavaClientPackage() + '.'
					+ introspectedTable.getFullyQualifiedTable().getDomainObjectName() + "Mapper"));
			superClass.addTypeArgument(new FullyQualifiedJavaType(
					introspectedTable.getFullyQualifiedTable().getDomainObjectName() + "Mapper"));
			topLevelClass.addImportedType(new FullyQualifiedJavaType(configuration.getJavaModelPackage() + '.'
					+ introspectedTable.getFullyQualifiedTable().getDomainObjectName()));
			superClass.addTypeArgument(
					new FullyQualifiedJavaType(introspectedTable.getFullyQualifiedTable().getDomainObjectName()));
			topLevelClass.addImportedType(new FullyQualifiedJavaType(configuration.getJavaModelPackage() + '.'
					+ introspectedTable.getFullyQualifiedTable().getDomainObjectName() + "Example"));
			superClass.addTypeArgument(new FullyQualifiedJavaType(
					introspectedTable.getFullyQualifiedTable().getDomainObjectName() + "Example"));

			topLevelClass.setSuperClass(superClass);

		}

		List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
		// if (context.getPlugins().modelBaseRecordClassGenerated(topLevelClass,
		// introspectedTable)) {
		answer.add(topLevelClass);
		// }
		return answer;
	}

	private String getServiceType() {
		String pakkage = configuration.getTargetPackage();
		StringBuilder sb = new StringBuilder();
		sb.append(pakkage);
		sb.append('.');
		sb.append(introspectedTable.getFullyQualifiedTable().getDomainObjectName());
		sb.append("Service"); //$NON-NLS-1$
		return sb.toString();

	}

	private FullyQualifiedJavaType getSuperClass() {
		FullyQualifiedJavaType superClass;

		String rootClass = configuration.getRootClass();
		if (rootClass != null) {
			superClass = new FullyQualifiedJavaType(rootClass);
		} else {
			superClass = null;
		}

		return superClass;
	}

}
